package  
{
	import EventDispatchers.XMLLoader;
	
	import Events.XMLEvent;
	
	import Graphics.*;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class GameObject
	{
		protected var level:Level;
		private var blitRectangle:Rectangle;
		
		protected var position:Point = new Point(0,0);
		protected var worldPosition:Point = new Point(0,0);
		protected var velocity:Point;
		
		// 0 - UP, 1 - Right, 2 - Down , 3 - Left
		protected var direction:Array = new Array(0,1,2,3);
		protected var currentDirection:int = 0;
		
		
		//animation stuff
		protected var animations:Array;
		protected var currentAnimation:Animation;
		protected var frameTimer:int;
		protected var currentFrame:int;
		protected var finished:Boolean;
		
		protected var width:int;
		protected var height:int;
		//is sprite active
		protected var isVisible:Boolean;
		
		public function GameObject(game:Level, xml:String)
		{
			this.level = game;
			LoadXML(xml);
			animations = new Array();
		}
		
		private function LoadXML(xml:String):void
		{
			var loader:XMLLoader = new XMLLoader();
			loader.addEventListener(XMLEvent.XMLLOADED, info);
			loader.load(xml);
		}
		
		private function info(e:XMLEvent):void
		{
			var xml:XML = new XML(e.Data);
			this.width = (int)(xml.width.toString());
			this.height = (int)(xml.height.toString());
			this.velocity = new Point((int)(xml.speed.attribute("x").toString()),(int)(xml.speed.attribute("y").toString()));
			
			blitRectangle = new Rectangle(0,0, width, height);
			for(var index:int = 0; index < xml.animations.animation.length(); index++)
			{
				var bool:Boolean = new Boolean((int)(xml.animations.animation[index].attribute("loop").toString()));
				var animation:Animation = new Animation(this,xml.animations.animation[index].attribute("name").toString(),xml.animations.animation[index].attribute("tileSheet").toString(),(int)(xml.animations.animation[index].attribute("frameNumber").toString()),bool, (int)(xml.animations.animation[index].attribute("fps").toString()));
				animations.push(animation);
			}
			currentAnimation = animations[0];
		}
		
		public function Update(deltaTime:int):void
		{
			Animate(deltaTime);
		}
		
		private function Animate(deltaTime:int):void
		{
			if((currentAnimation != null) &&(currentAnimation.FrameTime > 0) &&(currentAnimation.Loop || !finished))
			{
				frameTimer += deltaTime;
				while(frameTimer > currentAnimation.FrameTime)
				{
					frameTimer = frameTimer - currentAnimation.FrameTime;
					if(currentFrame == currentAnimation.FrameNumber)
					{
						if(currentAnimation.Loop)
							currentFrame = 0;
						else
							finished = true;
					}
					else
					{
						currentFrame ++;
					}
					blitRectangle.x = currentFrame * width;
					//blitRectangle.y = currentDirection * height;
				}
			}
		}
		public function Draw(canvas:BitmapData):BitmapData
		{
			if(currentAnimation != null)
			{
				canvas.copyPixels(currentAnimation.Graphic.bitmapData, blitRectangle, position);
			}
			return canvas;
		}
		public function Destroy():void
		{
			
		}
		
		public function setPosition(x:int, y:int):void
		{
			worldPosition.x = x * level.TileSize;
			worldPosition.y = y * level.TileSize;
		}
		
		protected function Move(futurePosition:Point):void
		{
		}
		
		public function get Width():int{return width;}
		public function get Height():int {return height;}
		public function get Position():Point {return position;}
		public function get WorldPosition():Point {return worldPosition;}
	}
}